package com.usian.controller;

import com.usian.feign.SSOServiceFeign;
import com.usian.pojo.TbUser;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author chen
 * @date 2021/1/21 - 20:08 - 星期四
 * @project usian_parent
 * @package com.usian.controller
 * @by GGbomd猪猪侠
 */
@RestController
@RequestMapping("/frontend/sso")
public class SSOControllerWeb {

    @Autowired
    private SSOServiceFeign SSOServiceFeign;

    /**
     * 对用户的注册信息(用户名与电话号码)做数据校验
     * @param checkValue 用户名 和手机号
     * @param checkFlag
     * @return
     */
    @RequestMapping("/checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue, @PathVariable Integer checkFlag){
        boolean b = SSOServiceFeign.checkUserInfo(checkValue, checkFlag);
        if (b){
            return Result.ok(b);
        }
        return Result.error("校验失败");
    }

    /**
     * 用户注册 相当于添加返回一条数据
     * @param tbUser
     * @return
     */
    @RequestMapping("/userRegister")
    public Result userRegister(TbUser tbUser){
        Integer integer = SSOServiceFeign.userRegister(tbUser);
        if (integer==1){
            return Result.ok();
        }
        return Result.error("注册失败");
    }

    /**
     * u用户登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/userLogin")
    public Result userLogin(String username, String password){
        Map map = SSOServiceFeign.userLogin(username, password);
        if (map != null && map.size() > 0){
            return Result.ok(map);
        }
        return Result.error("登录失败");
    }

    /**
     * 查询用户登录是否已经过期
     * @param token
     * @return
     */
    @RequestMapping("/getUserByToken/{token}")
    public Result getUserByToken (@PathVariable String token){
        TbUser userByToken = SSOServiceFeign.getUserByToken(token);
        if (userByToken != null){
            return Result.ok();
        }
        return Result.error("登录过期");
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @RequestMapping("/logOut")
    public Result logOut(String token){
        Boolean aBoolean = SSOServiceFeign.logOut(token);
        if (aBoolean){
            return Result.ok();
        }
        return Result.error("退出失败");
    }
}
